package pacman.util;

import java.util.LinkedList;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;

import pacman.base.Entity;

public class StateManager {

    private LinkedList<State> states;
    private State currentState;
    private State previousState;
    
    public StateManager() {
		states = new LinkedList<State>();
		currentState = null;
    }
    
    public void render(Graphics g) {
    	currentState.render(g);
    }

    public void update(GameContainer gc, int delta) {
    	currentState.update(gc, delta);
    }
    
    public void add(State s) {
		states.add(s);
		if (currentState == null) {
		    currentState = s;
		    currentState.enter();
		}
    }

    public void enter(Object o) {
		for (State s : states) {
		    if (s.equals(o)) {
				previousState = currentState;
				currentState = s;
				currentState.enter();
		    }
		}
    }

    public void onCollision(Entity obstacle) {
    	currentState.onCollision(obstacle);
    }

    public State currentState() {
    	return currentState;
    }

    public State getPreviousState() {
    	return previousState;
    }

}
